/**   
 * @Title: LotteryRecordDetailAct.java 
 * @Package com.esun.cheetah.view.personal.lotteryrecord
 * @Description: (购彩记录详情) 
 * @author 孔祥攀 <br/>
 * @date 2012-4-26 上午10:29:10
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.personal.lotteryrecord;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.lotteryrecord.LotteryRecordDetailFootballCommunicater;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.model.personal.LotteryRecordDetailFootballInfo;
import com.esun.cheetah.model.personal.LotteryRecordDetailFootballInfoBSXQ;
import com.esun.cheetah.network.ConnectionChangeReceiver;
import com.esun.util.ImageZhanJi;
import com.esun.util.ShowZhanji;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package 
 *          com.esun.cheetah.view.personal.lotteryrecord.LotteryRecordDetailFootballAct
 * @ClassName: LotteryRecordDetailFootballAct
 * @Description: TODO(购彩记录详情)
 * @author 孔祥攀 <br/>
 * @date 2012-4-26 上午10:29:10
 * 
 */
public class LotteryRecordDetailFootballAct extends Activity implements
		OnClickListener {

	private Button btnBack;
	private TextView tvPlayName;
	private TextView tvAddTime;
	private TextView tvProID;

	private TextView tvFaQiRen;
	private TextView tvLotteryMoney;
	private TextView tvLotteryCount;

	private TextView tvAllMoney;
	private TextView tvBeiShu;
	private TextView tvEachMoney;
	private TextView tvFenSHu;
	private TextView tvGGType;
	private TextView tvIssuc;
	private TextView tvState;
	private TextView tvGetMoney;
	private TextView tvJinDu;
	private TextView tvBaoDiMoney;
	private TextView tvTiCheng;

	private LinearLayout linearZhanJi;
	private LinearLayout linearProgramContent;
	private LinearLayout linearZhanJiWai;
	private LinearLayout linearHeMai;

	private LotteryRecordDetailFootballCommunicater footballDetailCommunicater;

	private LotteryRecordDetailFootballInfo listInfo;

	private Handler handlerGetLotteryRecordDetail;
	private ProgressDialog progressDialog;

	private String serverMessage;
	// private String playID;
	private String lotid;
	private String id;
	// private String uid;
	// private String fauserName;

	// private boolean isShow = true;
	// private boolean isShowTXT = false;
	private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotteryrecord_detailfootball_act);
		initHandler();
		initUI();

	}

	// 初始化
	public void initUI() {

		progressDialog = ViewerUtil.showProgressDialog(
				LotteryRecordDetailFootballAct.this,
				getResources().getString(R.string.loading_text));
		progressDialog.show();

		bundle = this.getIntent().getExtras();
		lotid = bundle.getString("lotid");
		id = bundle.getString("id");
		// uid = bundle.getString("uid");

		listInfo = new LotteryRecordDetailFootballInfo();

		new ImageZhanJi();

		btnBack = (Button) findViewById(R.id.lotteryrecord_detailfootball_act_btn_back);
		btnBack.setOnClickListener(this);

		tvPlayName = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_playType);
		tvProID = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_proid);
		tvAddTime = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_lotteryTime);
		tvFaQiRen = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_faQIRen);
		tvLotteryMoney = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_winMoney);
		tvLotteryCount = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_winCount);
		tvAllMoney = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_proMoney);
		tvBeiShu = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_beiShu);
		tvEachMoney = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_eachMoney);
		tvFenSHu = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_fenShu);
		tvGGType = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_ggType);
		tvIssuc = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_Issuc);
		tvState = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_lotteryState);
		tvGetMoney = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_getMoney);
		tvBaoDiMoney = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_baoDiMoney);
		tvTiCheng = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_tiCheng);
		tvJinDu = (TextView) findViewById(R.id.lotteryrecord_detailfootball_act_tv_jinDu);

		linearZhanJi = (LinearLayout) findViewById(R.id.lotteryrecord_detailfootball_act_linear_zhanJi);
		linearProgramContent = (LinearLayout) findViewById(R.id.lotteryrecord_detailfootball_act_linear_proContent);
		linearZhanJiWai = (LinearLayout) findViewById(R.id.lotteryrecord_detailfootball_act_linear_zhanJiWai);
		linearHeMai = (LinearLayout) findViewById(R.id.lotteryrecord_detailfootball_act_linear_heMai);

		getLotteryRecordDetailThread();
	}

	// 获取详细信息线程
	public void getLotteryRecordDetailThread() {
		footballDetailCommunicater = new LotteryRecordDetailFootballCommunicater(
				handlerGetLotteryRecordDetail, lotid, id);
		footballDetailCommunicater.startCommunicate();

	}

	// 通讯结果处理
	public void initHandler() {
		handlerGetLotteryRecordDetail = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case Config.COMMUNICATER_SUCCESS:
					listInfo = footballDetailCommunicater.getLotteryList();
					communicaterSucceed(listInfo);

					break;
				case Config.COMMUNICATER_FAIL:

					ViewerUtil.showTipDialog(
							LotteryRecordDetailFootballAct.this, serverMessage,
							false);

					break;
				case Config.COMMUNICATER_EXCEPTION:

					ViewerUtil.showTipDialog(
							LotteryRecordDetailFootballAct.this,
							Language.STR_CONNECTIONFAIL, false);

					break;

				default:
					break;
				}
				progressDialog.dismiss();
			}
		};
	}

	// 详情显示
	public void communicaterSucceed(LotteryRecordDetailFootballInfo list) {
		// 是否合买
		if (list.ishm.equals("0")) {
			linearHeMai.setVisibility(View.GONE);
		} else {
			tvBaoDiMoney.setText("￥" + list.baodimoney);
			tvTiCheng.setText(list.maingetmoney + "%");
			tvJinDu.setText(list.renqi + "%");

		}
		tvFenSHu.setText(list.anumber + "份");

		// fauserName = list.username;

		// playID = list.playid;

		tvLotteryCount.setText(list.prizecount + "次");
		tvAllMoney.setText("￥" + list.prizemoney);

		tvPlayName.setText(list.playname);
		tvAddTime.setText(list.addtime);
		tvProID.setText(list.proid);
		tvFaQiRen.setText(list.username);
		tvLotteryMoney.setText("￥" + list.allmoney);

		tvBeiShu.setText(list.beishu + "倍");
		tvEachMoney.setText("￥" + list.onemoney);
		if (list.issuc.equals("0")) {
			tvIssuc.setText("未出票");
		} else if (list.issuc.equals("1")) {
			tvIssuc.setText("已出票");
		}
		tvGGType.setText(list.ggtypename);
		if (list.isprizesuc.equals("0")) {// 未开奖
			tvState.setText("等待开奖");
		} else if (list.getmoney.equals("0")) {// 未中奖
			tvState.setText("未中奖");
		} else if (list.isprizesuc.equals("1")) {// 已派奖
			tvState.setText("已派奖");
			tvGetMoney
					.setText("￥"
							+ list.getmoney.substring(0,
									list.getmoney.indexOf(".") + 3));
		}
		zhanJiShow(list);

		programContentDeal(list);

	}

	/**
	 * 战绩计算和显示
	 * 
	 * @param list
	 */
	public void zhanJiShow(LotteryRecordDetailFootballInfo list) {
		// 金钻
		String golddiamond = list.jznum;
		// 银钻
		String silverdiamond = list.yznum;
		// 金星
		String goldstar = list.jxnum;
		// 银星
		String silverstar = list.yxnum;

		ShowZhanji zhanji = new ShowZhanji(LotteryRecordDetailFootballAct.this);
		// 获取显示战绩星级图片的list
		List<ImageView> zhanjilist = zhanji.showzhanji(golddiamond,
				silverdiamond, goldstar, silverstar);
		// 判断是否有战绩
		int size = zhanjilist.size();
		if (size == 0) {// 没有战绩就不显示战绩的linear
			linearZhanJiWai.setVisibility(View.GONE);
		} else {// 有战绩就将list中的ImageView添加到显示战绩的linear
			for (int i = 0; i < zhanjilist.size(); i++) {
				linearZhanJi.addView(zhanjilist.get(i));
			}
		}
	}

	// 方案内容显示处理
	private void programContentDeal(LotteryRecordDetailFootballInfo list) {

		ArrayList<LotteryRecordDetailFootballInfoBSXQ> teamArr = list.teamArr;

		if (teamArr != null && teamArr.size() == 0) {
			TextView tv_programContentDeal = new TextView(
					LotteryRecordDetailFootballAct.this);
			tv_programContentDeal.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tv_programContentDeal.setText("无内容");
			tv_programContentDeal.setTextSize(14);
			tv_programContentDeal.setTextColor(R.color.gcjl_detail_state);
			linearProgramContent.addView(tv_programContentDeal);
		} else {
			for (LotteryRecordDetailFootballInfoBSXQ info : teamArr) {
				View ll_touzhuInfo = View.inflate(
						LotteryRecordDetailFootballAct.this,
						R.layout.lotteryrecord_detailfootball_procontent_table,
						null);

				TextView tv_homesxname = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_homesxname);
				TextView tv_awaysxname = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_awaysxname);
				TextView tv_rangqiu = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_rangqiu);
				TextView tv_score = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_score);
				TextView tv_result = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_result);
				TextView tv_peilv = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_peilv);
				TextView tv_spf = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_spf);
				TextView tv_peilv2 = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_peilv2);
				TextView tv_spf2 = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_spf2);
				TextView tv_peilv3 = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_peilv3);
				TextView tv_spf3 = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_spf3);
				TextView tv_isdanma = (TextView) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_isdanma);
				LinearLayout ll_1 = (LinearLayout) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_ll_1);
				LinearLayout ll_2 = (LinearLayout) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_ll_2);
				LinearLayout ll_3 = (LinearLayout) ll_touzhuInfo
						.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_ll_3);

				// 队名多过五个字的后面以..显示
				String zhu;// 主队
				String ke;// 客队
				if (info.homesxname.length() >= 6) {
					zhu = info.homesxname.substring(0, 6) + "..";
				} else {
					zhu = info.homesxname;
				}
				if (info.awaysxname.length() >= 6) {
					ke = info.awaysxname.substring(0, 5) + "..";
				} else {
					ke = info.awaysxname;
				}
				tv_homesxname.setText(zhu);
				tv_awaysxname.setText(ke);

				if (info.rangqiu.equals("0")) {
					tv_rangqiu.setText("");
				} else {
					tv_rangqiu.setText("(" + info.rangqiu + ")");
				}

				if (info.score.equals("")) {
					tv_score.setText("VS");
				} else {
					tv_score.setText(info.score);
				}

				if (info.result.equals("")) {
					tv_result.setText("[未开]");
				} else {
					tv_result.setText("[" + info.result + "]");
				}

				String result = info.result;// 结果
				String peilv = info.peilv;// 赔率
				String[] peilvs = peilv.split(",");
				String spf = info.spf;// 用户投注
				String[] spfs = spf.split(",");

				for (int i = 0; i < spfs.length; i++) {
					if (spfs[i].equals("胜")) {
						tv_spf.setText(spfs[i]);
						tv_peilv.setText(peilvs[i]);
						tv_spf.setVisibility(View.VISIBLE);
						tv_peilv.setVisibility(View.VISIBLE);
						ll_1.setVisibility(View.VISIBLE);
						if (result.equals(spfs[i])) {
							tv_spf.setTextColor(this.getResources().getColor(
									R.color.red));
							tv_peilv.setTextColor(this.getResources().getColor(
									R.color.red));
						}
					} else if (spfs[i].equals("平")) {
						tv_spf2.setText(spfs[i]);
						tv_peilv2.setText(peilvs[i]);
						tv_spf2.setVisibility(View.VISIBLE);
						tv_peilv2.setVisibility(View.VISIBLE);
						ll_2.setVisibility(View.VISIBLE);
						if (result.equals(spfs[i])) {
							tv_spf2.setTextColor(this.getResources().getColor(
									R.color.red));
							tv_peilv2.setTextColor(this.getResources()
									.getColor(R.color.red));
						}
					} else if (spfs[i].equals("负")) {
						tv_spf3.setText(spfs[i]);
						tv_peilv3.setText(peilvs[i]);
						tv_spf3.setVisibility(View.VISIBLE);
						tv_peilv3.setVisibility(View.VISIBLE);
						ll_3.setVisibility(View.VISIBLE);
						if (result.equals(spfs[i])) {
							tv_spf3.setTextColor(this.getResources().getColor(
									R.color.red));
							tv_peilv3.setTextColor(this.getResources()
									.getColor(R.color.red));
						}
					}
				}

				if (info.isdanma.equals("1")) {
					tv_isdanma.setVisibility(View.VISIBLE);
				}
				EDebug.o(this, info.isdanma);

				linearProgramContent.addView(ll_touzhuInfo);

			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lotteryrecord_detailfootball_act_btn_back:

			LotteryRecordDetailFootballAct.this.finish();

			break;

		default:
			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// isShow = false;
		this.unregisterReceiver(ConnectionChangeReceiver
				.getConnectionChangeReceiver());
	}
//将menu键点击事件返回给父activity处理
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   // TODO Auto-generated method stub
   if (keyCode == KeyEvent.KEYCODE_MENU) {
     Activity parent = getParent();
     if (parent != null) {
       parent.onKeyDown(keyCode, event);
       return true;
     }
   }
   return super.onKeyDown(keyCode, event);
 }


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// isShow = true;
		this.registerReceiver(
				ConnectionChangeReceiver.getConnectionChangeReceiver(),
				ConnectionChangeReceiver.getIntentFilter());
	}
}